package tk.mybatis.springboot.limit;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;

public class TaskRun implements Runnable {
//    ArrayBlockingQueue<Integer> list = new ArrayBlockingQueue<>(100);
    static BlockingQueue<Integer> list = new LinkedBlockingQueue<>();
    SlidingWindowRateLimit rateLimit;

    public TaskRun(SlidingWindowRateLimit rateLimit) {
        this.rateLimit = rateLimit;
    }

    public boolean add(Integer a) {
        try {
             list.put(a);
             return true;
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return false;
    }

    @Override
    public void run() {

            for (;;) {
                System.out.println("tttt");
//                try {
//                    System.out.println(Thread.currentThread().getName() + " take -" + list.take());
//                    //如果空了，就会等待
//                } catch (InterruptedException e) {
//                    e.printStackTrace();
//                }

//            if (peek != null) {
//                while (true){
//                    if(rateLimit.canPass()){
//                        System.out.println("task run : " + peek);
//                        break;
//                    }else {
//                        try {
//                            Thread.sleep(200);
//                        } catch (InterruptedException e) {
//                            e.printStackTrace();
//                        }
//                    }
//
//                }
//            }
            }


    }
}
